package feedback.web.bean.student;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.primefaces.event.FlowEvent;
import org.springframework.beans.factory.annotation.Autowired;

import feedback.core.models.entities.Answer;
import feedback.core.models.entities.Question;
import feedback.core.models.entities.SurveyForm;
import feedback.core.models.entities.SurveyPart;
import feedback.core.models.entities.SurveyResult;
import feedback.core.services.EntityManagerServices;

@ManagedBean(name = "srb")
@SessionScoped
public class SurveyResulfBean {
	private boolean skip;
	private int rating;
	private String opinion;
	@Autowired
	private List<SurveyForm> listSurveyForms = new ArrayList<SurveyForm>();
	@Autowired
	private List<SurveyPart> listSurveyParts1 = new ArrayList<SurveyPart>();
	@Autowired
	private List<SurveyPart> listSurveyParts2 = new ArrayList<SurveyPart>();
	@Autowired
	private List<SurveyPart> listSurveyParts3 = new ArrayList<SurveyPart>();
	@Autowired
	private List<SurveyPart> listSurveyParts4 = new ArrayList<SurveyPart>();
	@Autowired
	private List<SurveyPart> listSurveyParts = new ArrayList<SurveyPart>();
	@Autowired
	private Question question;
	@Autowired
	private SurveyResult surveyResult;
	@Autowired
	private List<Question> listQT1 = new ArrayList<Question>();
	@Autowired
	private List<Question> listQT2 = new ArrayList<Question>();
	@Autowired
	private List<Question> listQT3 = new ArrayList<Question>();
	@Autowired
	private List<Question> listQT4 = new ArrayList<Question>();

	@ManagedProperty("#{lsj}")
	private ListSubject listSJ;
	@ManagedProperty("#{entityManager}")
	private EntityManagerServices servicesEn;

	@PostConstruct
	public void init() {

	}

	public List<SurveyPart> getSurveyPart() {
		listSurveyForms = (List<SurveyForm>) servicesEn
				.queryAll(SurveyForm.class);
		for (SurveyForm sf : listSurveyForms) {
			listSurveyParts.add((SurveyPart) sf.getSurveyPart());
		}
		return listSurveyParts;

	}

	public List<SurveyPart> getSurveyPart2() {
		listSurveyParts2.add(listSurveyParts.get(1));
		return listSurveyParts2;
	}

	public List<Question> getQuestion2() {
		for (SurveyPart sr : listSurveyParts2) {
			listQT2.add((Question) sr.getQuestions());
		}
		return listQT2;
	}

	public List<SurveyPart> getSurveyPart3() {
		listSurveyParts3.add(listSurveyParts.get(2));
		return listSurveyParts3;
	}
	public List<Question> getQuestion3() {
		for (SurveyPart sr : listSurveyParts3) {
			listQT3.add((Question) sr.getQuestions());
		}
		return listQT3;
	}

	public List<SurveyPart> getSurveyPart4() {
		listSurveyParts.add(listSurveyParts.get(3));
		return listSurveyParts4;
	}
	public List<Question> getQuestion4() {
		for (SurveyPart sr : listSurveyParts4) {
			listQT4.add((Question) sr.getQuestions());
		}
		return listQT4;
	}

	public List<SurveyPart> getSurveyPart1() {
		listSurveyParts1.add(listSurveyParts.get(0));
		return listSurveyParts1;
	}
	public List<Question> getQuestion1() {
		for (SurveyPart sr : listSurveyParts1) {
			listQT1.add((Question) sr.getQuestions());
		}
		return listQT1;
	}
	public String onFlowProcess(FlowEvent event) {
		if (skip) {
			skip = false; // reset in case user goes back
			return "confirm";
		} else {
			return event.getNewStep();
		}
	}
	public void saveRating(){
		Answer an = new Answer();
		an.setAnswerScore(rating);
		an.setQuestion(question);
		an.setSurveyResult(surveyResult);
		servicesEn.update(an);
	}
	public void saveSurveyResultOpinion(){
		SurveyResult sr = new SurveyResult();
		sr.setSurveyResultOpinion(opinion);
		servicesEn.update(sr);
	}
	
	//ket thuc ham

	public List<SurveyForm> getListSurveyForms() {
		return listSurveyForms;
	}

	public void setListSurveyForms(List<SurveyForm> listSurveyForms) {
		this.listSurveyForms = listSurveyForms;
	}

	public List<SurveyPart> getListSurveyParts1() {
		return listSurveyParts1;
	}

	public void setListSurveyParts1(List<SurveyPart> listSurveyParts1) {
		this.listSurveyParts1 = listSurveyParts1;
	}

	public List<SurveyPart> getListSurveyParts2() {
		return listSurveyParts2;
	}

	public void setListSurveyParts2(List<SurveyPart> listSurveyParts2) {
		this.listSurveyParts2 = listSurveyParts2;
	}

	public List<SurveyPart> getListSurveyParts3() {
		return listSurveyParts3;
	}

	public void setListSurveyParts3(List<SurveyPart> listSurveyParts3) {
		this.listSurveyParts3 = listSurveyParts3;
	}

	public List<SurveyPart> getListSurveyParts4() {
		return listSurveyParts4;
	}

	public void setListSurveyParts4(List<SurveyPart> listSurveyParts4) {
		this.listSurveyParts4 = listSurveyParts4;
	}

	public List<SurveyPart> getListSurveyParts() {
		return listSurveyParts;
	}

	public void setListSurveyParts(List<SurveyPart> listSurveyParts) {
		this.listSurveyParts = listSurveyParts;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	public ListSubject getListSJ() {
		return listSJ;
	}

	public void setListSJ(ListSubject listSJ) {
		this.listSJ = listSJ;
	}

	public EntityManagerServices getServicesEn() {
		return servicesEn;
	}

	public void setServicesEn(EntityManagerServices servicesEn) {
		this.servicesEn = servicesEn;
	}

	public List<Question> getListQT1() {
		return listQT1;
	}

	public void setListQT1(List<Question> listQT1) {
		this.listQT1 = listQT1;
	}

	public List<Question> getListQT2() {
		return listQT2;
	}

	public void setListQT2(List<Question> listQT2) {
		this.listQT2 = listQT2;
	}

	public List<Question> getListQT3() {
		return listQT3;
	}

	public void setListQT3(List<Question> listQT3) {
		this.listQT3 = listQT3;
	}

	public List<Question> getListQT4() {
		return listQT4;
	}

	public void setListQT4(List<Question> listQT4) {
		this.listQT4 = listQT4;
	}

	public SurveyResult getSurveyResult() {
		return surveyResult;
	}

	public void setSurveyResult(SurveyResult surveyResult) {
		this.surveyResult = surveyResult;
	}

	public boolean isSkip() {
		return skip;
	}

	public void setSkip(boolean skip) {
		this.skip = skip;
	}

	public int getRating() {
		return rating;
	}

	public void setRating(int rating) {
		this.rating = rating;
	}

	public String getOpinion() {
		return opinion;
	}

	public void setOpinion(String opinion) {
		this.opinion = opinion;
	}
}
